package com.cy.detectsys.controller.sys;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.cy.detectsys.config.CustomException;
import com.cy.detectsys.entity.SysMenu;
import com.cy.detectsys.mapper.SysMenuMapper;
import com.cy.detectsys.util.R;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("system")
public class MenuController {


    @Resource
    private SysMenuMapper menuMapper;

    @RequestMapping("menu")
    public R getmenu() {


        try {

            SysMenu menu = new SysMenu();

            List<SysMenu> allMenu = menuMapper.selectMenuListByUserIdBySystem(menu);


            Map<Integer, List<SysMenu>> collect = allMenu.stream().filter(menu1 -> ObjectUtil.isNotEmpty(menu1.getKySystem())).collect(Collectors.groupingBy(SysMenu::getKySystem));


            for (Integer systemId : collect.keySet()) {

                List<SysMenu> originMenus = collect.get(systemId);
                List<SysMenu> orderedMenus = menuOrdering(originMenus);
                collect.replace(systemId, orderedMenus);
            }
            return R.ok(collect);

        } catch (Exception e) {
            throw new CustomException("menu菜单查询异常" + e.getMessage());
        }


    }

    public List<SysMenu> menuOrdering(List<SysMenu> originList) {
        //查询根节点
        List<SysMenu> rootMenu = new ArrayList<>();
        for (SysMenu nav : originList) {
            if (nav.getKySysParentId().intValue() == 0) {
                //父节点为0，表示为根节点
                rootMenu.add(nav);
            }
        }
        //对根节点进行排序
        Collections.sort(rootMenu, order());
        //为跟菜单设置子菜单，getChild递归调用
        for (SysMenu nav : rootMenu) {
            List<SysMenu> childList = getChild(nav.getId().intValue(), originList);
            nav.setChildren(childList);
        }
        return rootMenu;
    }

    //菜单排序
    public Comparator<SysMenu> order() {
        Comparator<SysMenu> comparator = new Comparator<SysMenu>() {
            @Override
            public int compare(SysMenu o1, SysMenu o2) {
                if (o1.getKySysOrderNum() != o2.getKySysOrderNum()) {
                    return o1.getKySysOrderNum() - o2.getKySysOrderNum();
                }
                return 0;
            }
        };
        return comparator;
    }


    /**
     * @Description 获取子节点
     * @Author chen
     * @Date 2022/3/11 14:51
     * @Param id 父节点id
     * @Param allMenu 所有菜单列表
     */
    public List<SysMenu> getChild(Integer id, List<SysMenu> allMenu) {
        //子菜单
        List<SysMenu> childList = new ArrayList<>();
        for (SysMenu nav : allMenu) {
            if (nav.getKySysParentId().intValue() == id) {
                childList.add(nav);
            }
        }
        //递归
        for (SysMenu nav : childList) {
            nav.setChildren(getChild(nav.getId().intValue(), allMenu));
        }
        Collections.sort(childList, order());//排序
        if (childList.size() == 0) {
            return new ArrayList<SysMenu>();
        }
        return childList;
    }

}
